package com.tencent.cloud.uikit.utils;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.support.annotation.StringRes;
import android.support.annotation.StyleRes;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.tencent.cloud.iov.util.ResourcesUtils;
import com.tencent.cloud.iov.util.UtilServices;
import com.tencent.cloud.uikit.widget.ToolTip;
import com.tencent.cloud.uikit.R;

/**
 * @author vinsonhuang
 * @date 2018/6/8
 */
public class DialogUtils {

    private static Toast sToast;

    static {
        init(UtilServices.getContext());
    }

    private static void init(Context context) {
        sToast = new Toast(context);
        sToast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0);
        sToast.setDuration(Toast.LENGTH_LONG);
    }

    /**
     * 弹出对话框
     *
     * @param context     上下文
     * @param titleId     对话框标题资源ID
     * @param messageId   对话框内容资源ID
     * @param leftBtnText 对话框按钮标题
     */
    public static void showDialog(Context context, @StringRes int titleId,
                                  @StringRes int messageId, String leftBtnText) {
        showDialog(context, titleId, messageId, leftBtnText, null, null, null);
    }

    /**
     * 弹出对话框
     *
     * @param context     上下文
     * @param titleId     对话框标题资源ID
     * @param message     对话框内容资源
     * @param leftBtnText 对话框左边按钮标题
     */
    public static void showDialog(Context context, @StringRes int titleId,
                                  String message, String leftBtnText) {
        showDialog(context, titleId, message, leftBtnText, null, null, null);
    }

    /**
     * 弹出对话框
     *
     * @param context             上下文
     * @param titleId             对话框标题资源ID
     * @param messageId           对话框内容资源ID
     * @param leftBtnText         对话框左边按钮标题
     * @param rightBtnText        对话框右边按钮标题
     * @param leftButtonListener  对话框左边按钮监听器
     * @param rightButtonListener 对话框右边按钮监听器
     */
    public static void showDialog(Context context, @StringRes int titleId, @StringRes int messageId,
                                  String leftBtnText, String rightBtnText,
                                  DialogInterface.OnClickListener leftButtonListener,
                                  DialogInterface.OnClickListener rightButtonListener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(titleId)
                .setCancelable(false)
                .setMessage(messageId)
                .setNegativeButton(leftBtnText, leftButtonListener)
                .setPositiveButton(rightBtnText, rightButtonListener);
        builder.create().show();
    }

    /**
     * 弹出对话框
     *
     * @param context             上下文
     * @param titleId             对话框标题资源ID
     * @param messageId           对话框内容资源ID
     * @param leftTextId          对话框左边按钮标题资源ID
     * @param rightTextId         对话框右边按钮标题资源ID
     * @param leftButtonListener  对话框左边按钮监听器
     * @param rightButtonListener 对话框右边按钮监听器
     */
    public static void showDialog(Context context, @StringRes int titleId, @StringRes int messageId,
                                  @StringRes int leftTextId, @StringRes int rightTextId,
                                  DialogInterface.OnClickListener leftButtonListener,
                                  DialogInterface.OnClickListener rightButtonListener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(titleId)
                .setCancelable(false)
                .setMessage(messageId)
                .setNegativeButton(leftTextId, leftButtonListener)
                .setPositiveButton(rightTextId, rightButtonListener);
        builder.create().show();
    }

    /**
     * 弹出对话框
     *
     * @param context             上下文
     * @param titleId             对话框标题资源ID
     * @param message             对话框内容资源
     * @param leftBtnText         对话框左边按钮标题
     * @param rightBtnText        对话框右边按钮标题
     * @param leftButtonListener  对话框左边按钮监听器
     * @param rightButtonListener 对话框右边按钮监听器
     */
    public static void showDialog(Context context, @StringRes int titleId, String message,
                                  String leftBtnText, String rightBtnText,
                                  DialogInterface.OnClickListener leftButtonListener,
                                  DialogInterface.OnClickListener rightButtonListener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(titleId)
                .setCancelable(false)
                .setMessage(message)
                .setNegativeButton(leftBtnText, leftButtonListener)
                .setPositiveButton(rightBtnText, rightButtonListener);
        builder.create().show();
    }

    public static void showToast(Context context, @StringRes int message) {
        showToast(context, ResourcesUtils.getString(message));
    }

    public static void showToast(Context context, String message) {
        if (null == context || TextUtils.isEmpty(message)) {
            return;
        }
        if (context instanceof Activity) {
            Activity activity = (Activity) context;
            View toastRoot = activity.getLayoutInflater().inflate(R.layout.toast_layout, null);
            TextView tv = toastRoot.findViewById(R.id.tv_text_info);
            tv.setText(message);
            sToast.setView(toastRoot);
        } else {
            sToast.setText(message);
        }
        sToast.show();
    }

}
